<template>
  <div>
    <el-dialog :visible="isShowDialog" title="新增学科" width="500px" @close="onClose">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item label="是否显示">
          <el-switch
              v-model="form.isFrontDisplay"
              active-color="#13ce66"
              active-value="1"
              inactive-color="#ff4949"
              inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose()">取 消</el-button>
        <el-button type="primary" @click="addSubjectsName">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSubjects } from '@/api/hmmm/subjects.js'

export default {
  name: 'SubjectsAdd',
  props: {
    isShowDialog: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      // 添加学科表单
      form: {
        subjectName: '',
        isFrontDisplay: '1'
      },
      // 表单校验规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  methods: {
    // 关闭弹出框
    onClose () {
      this.form = {
        subjectName: '',
        isFrontDisplay: '1'
      }
      this.$refs.form.resetFields()
      this.$emit('update:isShowDialog', false)
    },
    // 添加学科
    async addSubjectsName () {
      try {
        await this.$refs.form.validate()
        await addSubjects(this.form)
        this.form = {
          subjectName: '',
          isFrontDisplay: '1'
        }
        this.$message.success('添加成功')
        this.$emit('loadSubjectsList')
        this.onClose()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang='less' scoped>
.el-form {
  padding-bottom: 10px;

  .el-input {
    width: 200px;
  }
}

</style>
